package cn.itsource.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetRedisLoginVoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.config.exception.BusinessException;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.dto.LoginDto;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.vo.LoginVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.List;

@Service //spring管理
public class EmployeeServiceImpl extends BaseServiceImpl<Employee,EmployeeQuery> implements IEmployeeService {
    //普通crud通过继承得到

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisService redisService;


    @Override
    public AjaxResult login(LoginDto loginDto) {

        //1.验证参数  @NotBlack

        //2.根据用户名进行匹配   //账户名与密码不匹配，请重新输入
        Employee employee = BeanUtil.copyProperties(loginDto, Employee.class);
        Employee dbEmployee = employeeMapper.checkRepeatEmployee(employee);

        //3.如果匹配不到，返回错误
        if (dbEmployee == null){
            throw new BusinessException("账户名与密码不匹配，请重新输入");
        }
        //4.如果匹配到了，对进来的密码串加盐加密和数据库的加密串进行比对，
        //4.1拿到数据库的盐值，再对页面传递过来的密码进行加密
        String pageMd5Pwd = MD5.create().setSalt(dbEmployee.getSalt().getBytes()).digestHex(loginDto.getPassword());

        //5.比对不过，返回错误  pageMd5Pwd:页面密码加密串   dbEmployee.getPassword():数据库的加密串
        if (!StrUtil.equals(pageMd5Pwd,dbEmployee.getPassword())){
            throw new BusinessException("账户名与密码不匹配，请重新输入");
        }

        //6.成功，存放redis.setKv(token,用户信息，时间)
        String token = IdUtil.fastSimpleUUID();
        //6.1封装成一个通用对象，用户系统和后台系统都可以用这个
        LoginVo loginVo = BeanUtil.copyProperties(dbEmployee, LoginVo.class);
        loginVo.setShopId(dbEmployee.getShopId());
        loginVo.setToken(token);

        redisService.setStringKeyAndValue(token,loginVo,30);
        //7.返回vo对象，vo专门用来返回值给前端页面的
        return AjaxResult.createSuccess(loginVo);

    }

    @Override
    public List<Employee> getShopEmployees() {

        Long shopId = GetRedisLoginVoUtil.getShopId();
        //根据店铺id查询店铺所有的员工
        return employeeMapper.getShopEmployees(shopId);
    }


    @Override
    public List<Employee> findManagers() {
        return employeeMapper.findManagers();
    }

    @Override
    public Employee checkRepeatEmployee(Employee admin) {
        return employeeMapper.checkRepeatEmployee(admin);
    }


}
